package jpsearch;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.sanselan.ImageReadException;
import org.apache.sanselan.common.BinaryFileFunctions;
import org.apache.sanselan.common.byteSources.ByteSource;
import org.apache.sanselan.common.byteSources.ByteSourceFile;
import org.apache.sanselan.formats.jpeg.JpegUtils;

public class JPSearchParser extends BinaryFileFunctions implements JPSearchConstants{

	private File jpsearchFile = null;
	private JPSearchSegment jpsearchData = new JPSearchSegment();

	public void testReadJPSearchData() throws Exception{
		jpsearchData.dumbJPSearchSegment();       
	}

	public JPSearchParser(String fileName){
		try {
			jpsearchFile = new File(fileName);  
			ByteSource byteSource = new ByteSourceFile(jpsearchFile);
			jpsearchData = getJPSearchMetadata(byteSource);

		} catch (ImageReadException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public JPSearchParser(ByteSource byteSource){

		try {
			if(getJPSearchMetadata(byteSource) != null)
				jpsearchData = getJPSearchMetadata(byteSource);
		} catch (ImageReadException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public byte [] getJPSearchSegmentNativeMetadata(){
		if(jpsearchData != null)
			return jpsearchData.getRawNativeMetadata();
		return null;
	}

	public byte [] getJPSearchSegmentXMLMetadata(){
		if(jpsearchData != null)
			return jpsearchData.getRawXMLMetadata();
		return null;
	}

	public String getXmlMetadata(){
		return jpsearchData.getData();
	}

	public File getJPSearchFile() {
		return jpsearchFile;
	}

	public JPSearchSegment getJpsearchData() {
		return jpsearchData;
	}

	@SuppressWarnings("rawtypes")
	private JPSearchSegment getJPSearchMetadata(ByteSource byteSource) throws ImageReadException, IOException {

		final List result = new ArrayList();

		JpegUtils.Visitor visitor = new JpegUtils.Visitor() {
			// return false to exit before reading image data.
			public boolean beginSOS()
			{
				return false;
			}

			public void visitSOS(int marker, byte markerBytes[],
					byte imageData[])
			{
			}

			// return false to exit traversal.
			@SuppressWarnings({ "unchecked" })
			public boolean visitSegment(int marker, byte markerBytes[],
					int markerLength, byte markerLengthBytes[],
					byte segmentData[]) throws ImageReadException, IOException {
				if (marker == 0xffd9)
					return false;

				if (marker == JPEG_APP3_Marker)
				{
					if (isJPSearchSegment(segmentData))
					{
						JPSearchSegment res = new JPSearchSegment(segmentData);
						result.add(res);
						return false;
					}
				}

				return true;
			}
		};
		new JpegUtils().traverseJFIF(byteSource, visitor);

		if(result.isEmpty())
			return null;
		else
			return (JPSearchSegment)result.get(0);
	}

	public static boolean isJPSearchSegment(byte segmentData[])
	{
		int i = 0;

		if (segmentData.length < JPSEARCH_IDENTIFIER.length) //change to JPSearch identifier
			return false;
		for (; i < JPSEARCH_IDENTIFIER.length; i++)
			if (segmentData[i] < JPSEARCH_IDENTIFIER[i])
				return false;

		return true;
	}

	//segmentData contains the whole JPSearch data segment (including ElementaryMetadata)
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void parseJPSearchSegment(byte segmentData[], List data) throws ImageReadException {

	}	
}
